﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcs.Web.Admin.mp
{
    public partial class listdis : Web.Admin.BasePage
    {
        Maticsoft.BLL.monthSupplayPlan bll = new Maticsoft.BLL.monthSupplayPlan();
        lcs.BLL.user bu = new BLL.user();
        BLL.configinfo bc = new BLL.configinfo();
        public string dogid = "NO";
        public string Decode(string str)
        {

            if (dogid == "NO")
            {
                if (Request.Cookies["dogid"]!=null)
                    dogid = Request.Cookies["dogid"].Value.ToString();
            }
            return DecodeDog(str, dogid);

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Model.configinfo c= bc.GetModel();
                lblmsg.Text =c.psbz.Value.ToString();
                int nums = bll.GetRecordCount(" user_id is null ");
                if (DateTime.Now.Day > c.psbz.Value && nums>0)
                    lblmsg.Text += "分配日期已过，还有未分配的任务请及时分配！";
                DataSet ds = bu.GetUserRoles(" roles_name='配送管理员'");
                ddldis.DataSource = ds.Tables[0];
                ddldis.DataTextField = "user_name";
                ddldis.DataValueField = "user_id";
                ddldis.DataBind();
                BindData();
                Page.DataBind();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
            string val = ddlstatus.SelectedItem.Text;
            if (val == "未分配")
            {
               dis.Visible=true;
                del.Visible=false;
                
            }
            else if (val == "已分配")
            {
                dis.Visible =false ;
                del.Visible = true;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            int uid = int.Parse(ddldis.SelectedValue.ToString());
            if(bll.Updateuid(uid, idlist))
            {
            BindData();
            Maticsoft.Common.MessageBox.Show(this, "分配成功!");
            }
        }

        #region gridView

        public void BindData()
        {
            #region
            //if (!Context.User.Identity.IsAuthenticated)
            //{
            //    return;
            //}
            //AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
            //if (user.HasPermissionID(PermId_Modify))
            //{
            //    gridView.Columns[6].Visible = true;
            //}
            //if (user.HasPermissionID(PermId_Delete))
            //{
            //    gridView.Columns[7].Visible = true;
            //}
            #endregion

            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            if (txtKeyword.Text.Trim() != "")
            {

                strWhere.AppendFormat(" and material like '%{0}%'", txtKeyword.Text.Trim());
            }
            if (txtKeyword2.Text.Trim() != "")
            {
                if (Decode("1") == "1")
                strWhere.AppendFormat(" and projectname like '%{0}%'", txtKeyword2.Text.Trim());
            }
            if (ddlstatus.SelectedIndex!=0)
            {
                if (ddlstatus.SelectedIndex==1)
                strWhere.Append(" and user_id is null");
                else
                    strWhere.Append(" and user_id is not null");
            }
            strWhere.Append(" and arrivalAddress is not null order by sname");
            ds = bll.GetListnums(strWhere.ToString());
            gridView.DataSource = ds;
            gridView.DataBind();
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }        
        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            //int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            //bll.Delete(ID);
            //gridView.OnBind();
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        #endregion

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbl_isGoodsQualityStatus = e.Row.FindControl("lbl_isGoodsQualityStatus") as Label;

                Label lbl_goodsQualityStatus = e.Row.FindControl("lbl_goodsQualityStatus") as Label;

                CheckBox cb = e.Row.FindControl("DeleteThis") as CheckBox;

                if (lbl_isGoodsQualityStatus.Text == "False")
                {
                    lbl_isGoodsQualityStatus.Text = "否";

                    cb.Enabled = true;
                }
                else
                {
                    lbl_isGoodsQualityStatus.Text = "是";

                    if (!string.IsNullOrEmpty(lbl_goodsQualityStatus.Text))
                    {
                        cb.Enabled = true;
                    }
                    else
                    {
                        cb.Enabled = false;
                    }
                    
                }
                
            }
        }

        protected void btndeldis_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            if (bll.Updateuid(0, idlist))
            {
                BindData();
                Maticsoft.Common.MessageBox.Show(this, "删除成功!");
            }
            else
            {
                Maticsoft.Common.MessageBox.Show(this, "删除失败，流程已处理!");

            }
        }
    }
}